package com.sangfor.ssl;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import com.gisocn.onemap.tdt.TDTConstant;
import com.sangfor.bugreport.BugReport;
import com.sangfor.ssl.common.VpnCommon;
import com.sangfor.ssl.easyapp.SangforNbAuth;
import com.sangfor.ssl.service.auth.AuthorManager;
import com.sangfor.ssl.service.setting.SettingManager;
import com.sangfor.ssl.service.utils.IGeneral;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* loaded from: classes2.dex */
public class SangforAuth implements IConstants {
    public static final int AUTH_MODULE_EASYAPP = 1;
    public static final int AUTH_MODULE_L3VPN = 2;
    private static final String TAG = "SangforAuth";
    private static SangforAuth instance;
    private int mUseModule = 2;
    private ISangforAuth mSangforAuthInstance = SangforNbAuth.getInstance();

    static {
        Log.i(TAG, "static initializer: load https and auth_forward libraries.");
        System.loadLibrary("https");
        System.loadLibrary("auth_forward");
    }

    private SangforAuth() {
    }

    public static SangforAuth getInstance() {
        SangforAuth sangforAuth;
        synchronized (SangforAuth.class) {
            if (instance == null) {
                instance = new SangforAuth();
            }
            sangforAuth = instance;
        }
        return sangforAuth;
    }

    public boolean setDelegate(IVpnDelegate iVpnDelegate) {
        if (iVpnDelegate == null) {
            com.sangfor.bugreport.logger.Log.error(TAG, "delegate can not be null!");
            return false;
        }
        this.mSangforAuthInstance.setDelegate(iVpnDelegate);
        return true;
    }

    public IVpnDelegate getDelegate() {
        return this.mSangforAuthInstance.getDelegate();
    }

    public int getModuleUsed() {
        return this.mUseModule;
    }

    public void changeVpnMode(int i) {
        this.mUseModule = i;
    }

    public String vpnGetSdkJavaVersion() {
        return SangforAuthManager.SDK_JAVA_VERSION;
    }

    public void init(Application application, Context context, IVpnDelegate iVpnDelegate, int i) throws SFException {
        BugReport.initAll(context);
        com.sangfor.bugreport.logger.Log.updateWriteLogLevel(context, 2);
        com.sangfor.bugreport.logger.Log.updateLogLevel(context, SangforAuthManager.getInstance().getLogLevel());
        com.sangfor.bugreport.logger.Log.info(TAG, "sdk version is " + vpnGetSdkVersion());
        com.sangfor.bugreport.logger.Log.info(TAG, "sdk java version is " + vpnGetSdkJavaVersion());
        clearSession();
        if (i == 1) {
            this.mSangforAuthInstance.init(application, context, iVpnDelegate, 1);
            this.mUseModule = 1;
        } else if (i == 2) {
            if (!(context instanceof Activity)) {
                throw new SFException("Can't Start L3vpn because context is not ACTIVITY!");
            }
            if (Build.VERSION.SDK_INT < 14) {
                throw new SFException("Can't Start L3vpn because android.os.Build.VERSION.SDK_INT < 14");
            }
            this.mSangforAuthInstance.init(application, context, iVpnDelegate, 2);
            this.mUseModule = 2;
        } else {
            throw new SFException("Unknow auth module. Can't init SangforAuth.");
        }
    }

    public boolean vpnInit(final long j, final int i) {
        if (j > 0 && i > 0) {
            new Thread(new Runnable() { // from class: com.sangfor.ssl.SangforAuth.1
                @Override // java.lang.Runnable
                public void run() {
                    com.sangfor.bugreport.logger.Log.saveVpnInfo("VPN:" + VpnCommon.longToIP(j) + "\r\nPORT:" + i + "\r\n", TDTConstant.TILE_MATRIX_SET_MERCATOR);
                }
            }).start();
            SettingManager.getInstance().setGlobalValue(SettingManager.GLOBAL_HOST_KEY, IGeneral.PROTO_HTTPS_HEAD + VpnCommon.longToIP(j) + ":" + i);
            return this.mSangforAuthInstance.vpnInit(j, i);
        }
        com.sangfor.bugreport.logger.Log.error(TAG, "vpn address is error, host:" + j + " port:" + i);
        return false;
    }

    public boolean setLoginParam(String str, String str2) {
        if (str != null && str.equals(IVpnDelegate.PASSWORD_AUTH_USERNAME)) {
            final String str3 = "USERNAME:" + str2 + "\r\n";
            new Thread(new Runnable() { // from class: com.sangfor.ssl.SangforAuth.2
                @Override // java.lang.Runnable
                public void run() {
                    com.sangfor.bugreport.logger.Log.saveVpnInfo(str3, "a");
                }
            }).start();
        }
        return this.mSangforAuthInstance.setLoginParam(str, str2);
    }

    public boolean vpnLogin(int i) {
        return this.mSangforAuthInstance.vpnLogin(i);
    }

    public boolean vpnLogout() {
        AuthorManager.getInstance().clearAuthorInfo();
        if (this.mSangforAuthInstance.vpnLogout()) {
            com.sangfor.bugreport.logger.Log.info(TAG, "call logout success");
            return true;
        }
        IVpnDelegate delegate = getDelegate();
        if (delegate != null) {
            delegate.vpnCallback(3, 6);
            com.sangfor.bugreport.logger.Log.info(TAG, "call logout failed");
            return true;
        }
        return true;
    }

    public boolean vpnCancelLogin() {
        return this.mSangforAuthInstance.vpnCancelLogin();
    }

    public boolean vpnQuit() {
        return this.mSangforAuthInstance.vpnQuit();
    }

    public String vpnGeterr() {
        return this.mSangforAuthInstance.vpnGeterr();
    }

    public int vpnGetErrorCode() {
        return this.mSangforAuthInstance.vpnGetErrorCode();
    }

    public void vpnSeterr(String str) {
        this.mSangforAuthInstance.vpnSeterr(str);
    }

    public String vpnGetSdkVersion() {
        return this.mSangforAuthInstance.vpnGetSdkVersion();
    }

    public int vpnQueryStatus() {
        return this.mSangforAuthInstance.vpnQueryStatus();
    }

    public int queryVpnStatus() {
        return this.mSangforAuthInstance.queryVpnStatus();
    }

    public int vpnGetRndCode() {
        return this.mSangforAuthInstance.vpnGetRndCode();
    }

    public void onActivityResult(int i, int i2) {
        this.mSangforAuthInstance.onActivityResult(i, i2);
    }

    public String getSmsPhoneNum() {
        return this.mSangforAuthInstance.getSmsPhoneNum();
    }

    public String getSmsCountDown() {
        return this.mSangforAuthInstance.getSmsCountDown();
    }

    public String getTwfid() {
        return this.mSangforAuthInstance.getTwfid();
    }

    public Context getContext() {
        return this.mSangforAuthInstance.getContext();
    }

    public String getPastAuthEmm() {
        return this.mSangforAuthInstance.getPastAuthEmm();
    }

    public String getLocalRclist(boolean z) {
        return this.mSangforAuthInstance.getLocalRclist(z);
    }

    public String getPasswordSafePolicyPrompt(String str) {
        int i;
        String[] strArr = {"当前密码不符合密码安全策略，必须修改密码:", "当前密码已过期，必须修改密码:", "为了您的账号安全，必须修改初始密码:", "密码长度不得小于", "密码不能包含用户名", "密码必须包含数字", "密码必须包含字母", "密码至少包含特殊英文字符 ~!@#$%^&*()_+ 中的一个", "新密码不能与旧密码完全相同"};
        HashMap hashMap = new HashMap();
        Matcher matcher = Pattern.compile("(\\w+)=\"*((?<=\")[^\"]+(?=\")|([^\\s]+))\"*").matcher(str);
        while (matcher.find()) {
            com.sangfor.bugreport.logger.Log.debug(TAG, "PasswordStrategy " + matcher.group(1) + "=" + matcher.group(2));
            hashMap.put(matcher.group(1), matcher.group(2));
        }
        String str2 = (String) hashMap.get("psw_errorCode");
        String str3 = (String) hashMap.get("psw_mask");
        String str4 = (String) hashMap.get("psw_minLen");
        int parseInt = !str2.equals("") ? Integer.parseInt(str2) : 0;
        int parseInt2 = !str3.equals("") ? Integer.parseInt(str3) : 0;
        int parseInt3 = !str4.equals("") ? Integer.parseInt(str4) : 0;
        String str5 = strArr[0];
        if ((parseInt & 2) != 0) {
            str5 = strArr[1];
        } else if ((parseInt & 16) != 0) {
            str5 = strArr[2];
        }
        StringBuilder sb = new StringBuilder(str5 + "\r\n");
        if ((parseInt2 & 1) != 0) {
            sb.append(String.format("%d、%s %s\r\n", 1, strArr[3], Integer.valueOf(parseInt3)));
            i = 1;
        } else {
            i = 0;
        }
        if ((parseInt2 & 8) != 0) {
            i++;
            sb.append(String.format("%d、%s\r\n", Integer.valueOf(i), strArr[4]));
        }
        if ((parseInt2 & 64) != 0) {
            i++;
            sb.append(String.format("%d、%s\r\n", Integer.valueOf(i), strArr[5]));
        }
        if ((parseInt2 & 128) != 0) {
            i++;
            sb.append(String.format("%d、%s\r\n", Integer.valueOf(i), strArr[6]));
        }
        if ((parseInt2 & 256) != 0) {
            i++;
            sb.append(String.format("%d、%s\r\n", Integer.valueOf(i), strArr[7]));
        }
        if ((parseInt2 & 512) != 0) {
            sb.append(String.format("%d、%s\r\n", Integer.valueOf(i + 1), strArr[8]));
        }
        String sb2 = sb.toString();
        com.sangfor.bugreport.logger.Log.debug(TAG, "PasswordStrategy " + sb2);
        return sb2;
    }

    public String vpnGetCertSubject(String str, String str2) {
        return this.mSangforAuthInstance.vpnGetCertSubject(str, str2);
    }

    public String getChallengeReply() {
        return getChallengeMessage();
    }

    public String getPasswordPolicy() {
        return this.mSangforAuthInstance.getPasswordPolicy();
    }

    public String getChallengeMessage() {
        return this.mSangforAuthInstance.getChallengeMessage();
    }

    public ChangePasswordResult changePassword(String str, String str2) {
        return this.mSangforAuthInstance.changePassword(str, str2);
    }

    public int vpnRegetSmsCodeBlock() {
        return this.mSangforAuthInstance.vpnRegetSmsCodeBlock();
    }

    public void clearSession() {
        this.mSangforAuthInstance.clearSession();
    }

    public boolean setDnsWhiteList(List<String> list) {
        return this.mSangforAuthInstance.setDnsWhiteList(list);
    }
}
